package oboe.functors
{
	import oboe.core.SigSlot;
	import oboe.math.Number3D;

	public class CollisionPoll
	{
		public var position:Number3D;
		private var collisionCorrector:Number3D;
		public var collisionData:Vector.<CollisionData>;
		public var hasCollision:Boolean;
		public var target:SigSlot;
		
		public function CollisionPoll()
		{
			this.collisionData = new Vector.<CollisionData>();
			this.collisionCorrector = new Number3D(0,0,0);
			this.hasCollision = false;
		}
		
		public function reset( target:SigSlot, pos:Number3D ):void
		{
			this.target = target;
			this.position = pos;
			this.collisionData.length = 0;
			this.collisionCorrector.copy(0);
			this.hasCollision = false;
		}
		
		public function addCollision( target:SigSlot, pos:Number3D, data:Object=null ):void
		{
			if( this.target == target ) return;
			if( isNaN(pos.x) || isNaN(pos.z) ) 
			{
				trace("NaN corrector colliding with ", Object(target).constructor );
				return;
			}
			this.hasCollision = true;
			this.collisionCorrector.add3( pos );
			this.collisionData.push( new CollisionData( target, pos, data ) );
		}
		
		public function calcCorrector():Number3D
		{
			this.collisionCorrector.div( this.collisionData.length );
			return this.collisionCorrector;
		}
	}
}